package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.BFeeModelEnum;
import cn.huiyunche.base.service.enums.BFeeTypeEnum;
import cn.huiyunche.base.service.interfaces.BFeeModelService;
import cn.huiyunche.base.service.interfaces.BFeeTypeService;
import cn.huiyunche.base.service.interfaces.CWaybillFeedetailService;
import cn.huiyunche.base.service.mappers.CWaybillFeedetailMapper;
import cn.huiyunche.base.service.model.BFeeModel;
import cn.huiyunche.base.service.model.BFeeType;
import cn.huiyunche.base.service.model.BFeeTypeExample;
import cn.huiyunche.base.service.model.CWaybillFeedetail;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 运单费用明细接口实现类
 *
 * @author Administrator
 */
@Service
public class CWaybillFeedetailServiceImpl implements CWaybillFeedetailService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CWaybillFeedetailServiceImpl.class);

    @Autowired
    private BFeeModelService bFeeModelService = null;

    @Autowired
    private BFeeTypeService bFeeTypeService = null;

    @Autowired
    private CWaybillFeedetailMapper cWaybillFeedetailMapper = null;

    private BFeeModelService getBFeeModelService() {
        return this.bFeeModelService;
    }

    private BFeeTypeService getBFeeTypeService() {
        return this.bFeeTypeService;
    }

    private CWaybillFeedetailMapper getCWaybillFeedetailMapper() {
        return this.cWaybillFeedetailMapper;
    }

    /**
     * 运单 费用明细
     *
     * @param id
     * @param cost
     * @return
     */
    @Override
    public int addCWaybillFeeDetails(Long id, BigDecimal cost, BigDecimal actualAmount) {
        LOGGER.info("create cwaybill add feedetails params id : {}, cost : {}, {}", id, cost, actualAmount);
        BFeeModel feeModel = this.getBFeeModelService().getByCode(BFeeModelEnum.INTERCITY_WAYBILL);

        BFeeTypeExample example = new BFeeTypeExample();
        example.createCriteria().andFeeModelIdEqualTo(feeModel.getId()).andFeeCodeEqualTo(BFeeTypeEnum.SHIPPING.getValue());
        BFeeType bFeeType = this.getBFeeTypeService().selectByExample(example);

        try {
            CWaybillFeedetail feedetail = new CWaybillFeedetail();
            feedetail.setCreateTime(new Date());
            feedetail.setUpdateTime(new Date());
            feedetail.setWaybillId(id);
            feedetail.setMoney(cost);
            feedetail.setActualAmount(actualAmount);
            feedetail.setFeeTypeId(bFeeType.getId());
            feedetail.setFeeName(bFeeType.getFeeName());
            feedetail.setFeeCode(bFeeType.getFeeCode());
            LOGGER.info("create cwaybill add feedetails params id : {}, feedetail : {}.", id, feedetail);

            return this.getCWaybillFeedetailMapper().insertSelective(feedetail);
        } catch (Exception e) {
            LOGGER.error("create cwaybill add feedetails error : {}.", e);
            throw new BusinessException("创建运单写入运单明细异常");
        }
    }

}
